﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace MT.CustomUserControl.ViewModels
{
    public class RadioComboxUcViewModel : ObservableObject
    {
        /// <summary>
        /// ComboxSelectedChangedActon
        /// </summary>
        public Action<int> ComboxSelectedChangedActon;

        /// <summary>
        /// ComboxCurSelectedIndex
        /// </summary>
        private int comboxCurSelectedIndex = -1;
        public int ComboxCurSelectedIndex
        {
            get { return comboxCurSelectedIndex; }
            set { SetProperty(ref comboxCurSelectedIndex, value); }
        }

        /// <summary>
        /// ComboxCurSelectedValue
        /// </summary>
        private string comboxCurSelectedValue = string.Empty;
        public string ComboxCurSelectedValue
        {
            get { return comboxCurSelectedValue; }
            set 
            {
                if (comboxCurSelectedValue != value)
                {
                    SetProperty(ref comboxCurSelectedValue, value);
                    if (comboxCurSelectedValue != string.Empty)
                        ComboxSelectedChangedActon?.Invoke(ComboxCurSelectedIndex);
                }                
            }
        }

        /// <summary>
        /// ComboxItems
        /// </summary>
        private ObservableCollection<string> comboxItemsSource = new ObservableCollection<string>();
        public ObservableCollection<string> ComboxItemsSource
        {
            get { return comboxItemsSource; }
            set 
            { 
                SetProperty(ref comboxItemsSource, value);
                if (ComboxItemsSource != null && ComboxItemsSource.Count > 0)
                {
                    ComboxItemsSourceListBack.Clear();
                    foreach (var item in ComboxItemsSource)
                    {
                        ComboxItemsSourceListBack.Add(item);
                    }
                }
            }
        }

        /// <summary>
        /// 选项备份，用于在其他线程调用
        /// </summary>
        public List<string> ComboxItemsSourceListBack = new List<string>();

        public RadioComboxUcViewModel( Action<int> action)
        {
            ComboxSelectedChangedActon = action;
        }

        public void InitAndReset()
        {
            ComboxCurSelectedValue = string.Empty;
            ComboxCurSelectedIndex = -1;
        }
    }
}
